﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataObjects.Interface;
using BusinessObjects;
using DataObjects.AdoNet;
using System.Data;

namespace DataObjects.SqlServer
{
    public class SqlServerRestaurantsDao : IRestaurantDao
    {
        public List<Restaurant> GetRestaurants()
        {
            string procedures = "Get_All_Restaurants";

            return Db.ReadList(procedures, true, Make);
        }

        public List<Restaurant> GetRestaurantsByPage(int currentPage)
        {
            string procedures = "Get_Paged_Restaurants";

            object[] parms = {  "@WhereClause", null, 
                                "@OrderBy", null ,
                                "@PageIndex", currentPage};
            return Db.ReadList(procedures, true, Make, parms);
        }

        public List<Restaurant> GetRestaurantsInclueField(string field)
        {
            string procedures = "Proc_Get_All_RestaurantsIncludeField";

            object[] parms = { "@Field", field };
            return Db.ReadList(procedures, true, MakeWithInclueField, parms);
        }

        public Restaurant GetRestaurant(string id)
        {
            string procedures = "Get_All_Restaurants_ByID";

            object[] parms = { "@RestaurantID", id };
            return Db.Read(procedures, true, Make, parms);
        }

        public Restaurant GetRestaurantByName(string name)
        {
            string procedures = "Get_All_Restaurants_ByName";

            object[] parms = { "@RestaurantName", name };
            return Db.Read(procedures, true, Make, parms);
        }

        public Restaurant GetRestaurantByPhoneNumber(string phoneNumber)
        {
            string procedures = "Get_All_Restaurants_ByPhoneNumber";

            object[] parms = { "@PhoneNumber", phoneNumber };
            return Db.Read(procedures, true, Make, parms);
        }

        public int GetTotalPage()
        {
            string procedures = "Get_PageSize_Restaurant";
            object[] parms = { "@WhereClause", null };
            return Db.ReadTotalPage(procedures, true, parms);
        }

        public void InsertRestaurant(Restaurant restaurant)
        {
            string sql = "Insert_Restaurant";

            restaurant.RestaurantID = Db.Insert(sql, true, Take(restaurant));
        }

        public void UpdateRestaurant(Restaurant restaurant)
        {
            string sql = "Update_Restaurant";

            Db.Update(sql, true, Take(restaurant));
        }

        public void DeleteRestaurant(Restaurant restaurant)
        {
            string sql = "Delete_Restaurant";

            Db.Update(sql, true, Take(restaurant.RestaurantID));
        }

        private object[] Take(string restaurantId)
        {
            return new object[]  
            {
                "@RestaurantID", restaurantId,
            };
        }

        private static Func<IDataReader, Restaurant> Make = reader =>
            new Restaurant
            {
                RestaurantID = reader["RestaurantID"].AsString(),
                RestaurantCode = reader["RestaurantCode"].AsString(),
                RestaurantName = reader["RestaurantName"].AsString(),
                Address = reader["Address"].AsString(),
                PhoneNumber = reader["PhoneNumber"].AsString(),
                Fax = reader["Fax"].AsString(),
                Email = reader["Email"].AsString(),
                Website = reader["Website"].AsString(),
                TaxCode = reader["TaxCode"].AsString(),
                IsActive = reader["IsActive"].AsBool(),
                IsAgencies = reader["IsAgencies"].AsBool(),
                Description = reader["Description"].AsString()
            };

        private static Func<IDataReader, Restaurant> MakeWithInclueField = (reader) =>
            new Restaurant
            {
                RestaurantID = reader["RestaurantID"].AsString(),
                RestaurantName = reader["RestaurantName"].AsString(),
                Address = reader["Address"].AsString(),
                PhoneNumber = reader["PhoneNumber"].AsString(),
            };

        private object[] Take(Restaurant restaurant)
        {
            return new object[]  
            {
                "@RestaurantID", restaurant.RestaurantID,
                "@RestaurantCode", restaurant.RestaurantCode,
                "@RestaurantName", restaurant.RestaurantName,
                "@Address", restaurant.Address,
			    "@PhoneNumber", restaurant.PhoneNumber,
                "@Fax", restaurant.Fax ,
                "@Email", restaurant.Email ,
                "@Website", restaurant.Website ,
                "@TaxCode", restaurant.TaxCode ,
                "@IsAgencies", restaurant.IsAgencies ,
                "@IsActive", restaurant.IsActive ,
                "@Description", restaurant.Description ,
            };
        }
    }
}
